﻿using System.Data;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Orchard.Environment.Extensions;
using Orchard.Indexing;

namespace Rework.CommerceHistory.Migrations {
    [OrchardFeature("Rework.CommerceHistory")]
    public class CommerceHistoryMigrations : DataMigrationImpl {

        public int Create() {
            SchemaBuilder.CreateTable("CommerceHistoryPartRecord", table => table
                .ContentPartRecord()
                .Column("UserId", DbType.Int32)
                .Column("ProductId", DbType.Int32)
                .Column("Quantity", DbType.Int32)
                .Column("Price", DbType.Double)
                .Column("IsPaymentConfirmed", DbType.Boolean)
                .Column("ConfirmedTransactionId", DbType.String)
                .Column("ConfirmedIPN", DbType.String, c => c.Unlimited())
            );

            ContentDefinitionManager.AlterPartDefinition("CommerceHistoryPart",
              builder => builder.Attachable());

            ContentDefinitionManager.AlterTypeDefinition("CommerceHistory", cfg => cfg
              .WithPart("CommerceHistoryPart")
              .WithPart("CommonPart")
              .Creatable());

            ContentDefinitionManager.AlterTypeDefinition("CommerceHistoryWidget", cfg => cfg
                .WithPart("CommerceHistoryPart")
                .WithPart("CommonPart")
                .WithPart("WidgetPart")
                .WithSetting("Stereotype", "Widget"));
            return 1;
        }
    }
}
